/**
 * @description 作品 数据操作
 */
const { NewsModel, NewsCategoryModel } = require("../models/NewsModel");
/**
 * 创建新闻
 */
async function createNewsService(data = {}) {
  const news = await NewsModel.create(data);
  return news;
}
/**
 * @description 创建新闻列表
 * @returns
 */
async function createNewsCategoryService(data = {}) {
  const news = await NewsCategoryModel.create(data);
  return news;
}
/**
 * 查询所有新闻
 */
async function findNewsListService() {
  const news = await NewsModel.find();
  return news;
}
/**
 * 根据新闻分类id查询所有新闻
 */
async function findNewsByCategoryIdService(id) {
  if (!id) {
    const newsCategory = await NewsCategoryModel.find({
      name: "公司新闻",
    });
    id = newsCategory[0]._id;
  }
  const news = await NewsModel.find({
    cId: id,
  });
  return news;
}
async function findNewsByIdService(id) {
  const news = await NewsModel.findOne({
    _id: id,
  });
  return news;
}
/**
 * 查询所有新闻列表
 */
async function findNewsCategoryListService() {
  const newsCategoryList = await NewsCategoryModel.find();
  return newsCategoryList;
}

module.exports = {
  findNewsListService,
  findNewsCategoryListService,
  createNewsService,
  createNewsCategoryService,
  findNewsByCategoryIdService,
  findNewsByIdService,
};
